/**
 *====================================================
 * 文件名称: FinanceMoneyStatementServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年12月17日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.FinanceMoneyStatementEntity;
import com.yuexunit.finance.project.entity.lexicon.PaymentChannelEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.StatementStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: FinanceMoneyStatementServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年12月17日 09:54:08
 */
@Implements(FinanceMoneyStatementService.class)
public class FinanceMoneyStatementServiceImpl implements FinanceMoneyStatementService {

	@Override
	@Transactional
	public Long createFinanceMoneyStatement(FinanceMoneyStatementEntity entity) {
		if (entity.getStatementState() == null) {
			entity.setStatementState(StatementStateEnum.WAIT_PAY);
		}
		if (entity.getPaymentChannel() == null) {
			entity.setPaymentChannel(PaymentChannelEnum.UNKNOWN);
		}
		if (entity.getCreditQuota() == null) {
			entity.setCreditQuota(BigDecimal.ZERO);
		}
		entity.setStatementNumber(SettingConstants.PRE_NUMBER_MONEY_STATEMENT + RandomUtil.generateTimeNumber());
		entity.setReceiptAmount(BigDecimal.ZERO);
		entity.setReceiptBalance(entity.getAmount().subtract(entity.getReceiptAmount()));
		entity.setReceiptVerifiState(Boolean.FALSE);
		entity.setDisbursementAmount(BigDecimal.ZERO);
		entity.setDisbursementBalance(entity.getAmount().subtract(entity.getDisbursementAmount()));
		entity.setDisbursementVerifiState(Boolean.FALSE);
		entity.setCreateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getMoneyStatementId();
	}

	@Override
	public void renovateFinanceMoneyStatement(FinanceMoneyStatementEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public FinanceMoneyStatementEntity inquireFinanceMoneyStatementDetail(Long moneyStatementId) {
		return PersistUtil.get(FinanceMoneyStatementEntity.class, moneyStatementId);
	}

	@Override
	public PageList<FinanceMoneyStatementEntity> inquireFinanceMoneyStatementPageList(Long financeId, String[] statementSource, Long receiptTenantId, String receiptName, Long disbursementTenantId, String disbursementName,
			String financeNumber, Integer beginStatementDate, Integer endStatementDate, BigDecimal minSourceAmount, BigDecimal maxSourceAmount, StatementStateEnum statementState, String statementNumber, Boolean receiptVerifiState,
			Boolean disbursementVerifiState, Integer productType, PageCriteria page) {
		List<Long> financeIds = null;
		if ( productType!= null && productType!=0) {// 若有搜索货物名称
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
			criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
			financeIds = PersistUtil.get(criteria);
			if (financeIds.isEmpty()) {
				return new PageList<>(page);
			}
		}
		
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		if (financeIds != null && financeIds.size() > 0) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		if (receiptTenantId != null) {
			criteria.add(Restrictions.eq("receiptTenantId", receiptTenantId));
		}
		if (StringUtils.isNotBlank(receiptName)) {
			criteria.add(Restrictions.like("receiptName", "%" + receiptName + "%"));
		}
		if (disbursementTenantId != null) {
			criteria.add(Restrictions.eq("disbursementTenantId", disbursementTenantId));
		}
		if (StringUtils.isNotBlank(disbursementName)) {
			criteria.add(Restrictions.like("disbursementName", "%" + disbursementName + "%"));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (beginStatementDate != null) {
			criteria.add(Restrictions.ge("statementDate", beginStatementDate));
		}
		if (endStatementDate != null) {
			criteria.add(Restrictions.le("statementDate", endStatementDate));
		}
		if (minSourceAmount != null) {
			criteria.add(Restrictions.ge("amount", minSourceAmount));
//			criteria.add(Restrictions.ge("sourceAmount", minSourceAmount));
		}
		if (maxSourceAmount != null) {
			criteria.add(Restrictions.le("amount", maxSourceAmount));
//			criteria.add(Restrictions.le("sourceAmount", maxSourceAmount));
		}
		if (statementState != null) {
			criteria.add(Restrictions.le("statementState", statementState));
		}
		if (receiptVerifiState != null) {
			criteria.add(Restrictions.eq("receiptVerifiState", receiptVerifiState));
		}
		if (disbursementVerifiState != null) {
			criteria.add(Restrictions.eq("disbursementVerifiState", disbursementVerifiState));
		}
		if (StringUtils.isNotBlank(statementNumber)) {
			criteria.add(Restrictions.like("statementNumber", "%" + statementNumber + "%"));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (statementSource != null && statementSource.length > 0) {
			criteria.add(Restrictions.in("statementSource", statementSource));
		}
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}


	@Override
	public List<FinanceMoneyStatementEntity> inquireFinanceMoneyStatementList(String[] statementSource,
			Integer beginStatementDate, Integer endStatementDate) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		if (beginStatementDate != null) {
			criteria.add(Restrictions.ge("statementDate", beginStatementDate));
		}
		if (endStatementDate != null) {
			criteria.add(Restrictions.le("statementDate", endStatementDate));
		}
		if (statementSource != null && statementSource.length > 0) {
			criteria.add(Restrictions.in("statementSource", statementSource));
		}
		criteria.add(Restrictions.or(Restrictions.eq("disbursementVerifiState", Boolean.TRUE), Restrictions.eq("receiptVerifiState", Boolean.TRUE)));
		criteria.addOrder(Order.asc("statementDate"));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireFinanceMoneyStatementList1(Long enterpriseId, String[] statementSource,
			Integer beginStatementDate, Integer endStatementDate) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		if (beginStatementDate != null) {
			criteria.add(Restrictions.ge("statementDate", beginStatementDate));
		}
		if (endStatementDate != null) {
			criteria.add(Restrictions.le("statementDate", endStatementDate));
		}
		if (statementSource != null && statementSource.length > 0) {
			criteria.add(Restrictions.in("statementSource", statementSource));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.or(Restrictions.eq("disbursementId", enterpriseId), Restrictions.eq("receiptId", enterpriseId)));
		}
		//		criteria.add(Restrictions.eq("receiptBalance", BigDecimal.ZERO));
		criteria.addOrder(Order.asc("statementDate"));
		return PersistUtil.get(criteria);
	}

	@Override
	public FinanceMoneyStatementEntity inquireFinanceMoneyStatementDetail(String[] statementSource, Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		if (statementSource != null && statementSource.length > 0) {
			criteria.add(Restrictions.in("statementSource", statementSource));
		}
		criteria.add(Restrictions.eq("financeId", financeId));
		criteria.add(Restrictions.eq("receiptVerifiState", Boolean.FALSE));
		List<FinanceMoneyStatementEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public FinanceMoneyStatementEntity inquireFinanceMoneyStatementDetail1(String[] statementSource, Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		if ((statementSource != null) && (statementSource.length > 0)) {
			criteria.add(Restrictions.in("statementSource", statementSource));
		}
		criteria.add(Restrictions.eq("financeId", financeId));
		List<FinanceMoneyStatementEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return ((FinanceMoneyStatementEntity)list.get(0));
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireFinanceMoneyStatementList(Collection<Long> moneyStatementIds) {
		if (moneyStatementIds == null || moneyStatementIds.isEmpty()) {
			return null;
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.in("moneyStatementId", moneyStatementIds));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireFinanceMoneyStatementList(Long receiptTenantId, Long disbursementTenantId, Long[] moneyStatementId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		if (receiptTenantId != null) {
			criteria.add(Restrictions.eq("receiptTenantId", receiptTenantId));
		}
		if (disbursementTenantId != null) {
			criteria.add(Restrictions.eq("disbursementTenantId", disbursementTenantId));
		}
		criteria.add(Restrictions.in("moneyStatementId", moneyStatementId));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireFinanceMoneyStatementList(Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		if (financeId != null) {
			if (financeId != null && financeId != 0L) {
				criteria.add(Restrictions.eq("financeId", financeId));
			}
		}
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countreceiptConfirm() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.in("statementSource", new String[] { "BAIL_ADDITION" }));
		criteria.add(Restrictions.eq("receiptVerifiState", Boolean.TRUE));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("moneyStatementId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireFinanceMoneyStatementBillReceivableList(Long billReceivableId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.eq("billReceivableId", billReceivableId));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireFinanceMoneyReceiptStatementList(Long receiptTenantId, Long financeId, String[] statementSources) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		if (receiptTenantId != null) {
			criteria.add(Restrictions.eq("receiptTenantId", receiptTenantId));
		}
		criteria.add(Restrictions.in("statementSource", statementSources));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireFinanceMoneyDisbursementStatementList(Long disbursementTenantId, Long financeId, String[] statementSources) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		if (disbursementTenantId != null) {
			criteria.add(Restrictions.eq("disbursementTenantId", disbursementTenantId));
		}
		criteria.add(Restrictions.in("statementSource", statementSources));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireExceptFinanceMoneyReceiptStatementList(Long receiptTenantId, Long financeId, String[] statementSources) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		criteria.add(Restrictions.eq("receiptTenantId", receiptTenantId));
		criteria.add(Restrictions.not(Restrictions.in("statementSource", statementSources)));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireExceptFinanceMoneyDisbursementStatementList(Long disbursementTenantId, Long financeId, String[] statementSources) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		criteria.add(Restrictions.eq("disbursementTenantId", disbursementTenantId));
		criteria.add(Restrictions.not(Restrictions.in("statementSource", statementSources)));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitReceipt(Long tenantId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.eq("receiptTenantId", tenantId));
		criteria.add(Restrictions.ne("receiptBalance", BigDecimal.ZERO));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("moneyStatementId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitLoan(Long tenantId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.eq("disbursementTenantId", tenantId));
		criteria.add(Restrictions.ne("disbursementBalance", BigDecimal.ZERO));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("moneyStatementId")));
		return PersistUtil.get(criteria);
	}

	@Override
	@Transactional
	public void deleteFinanceMoneyStatement(Long moneyStatementId) {
		PersistUtil.delete(FinanceMoneyStatementEntity.class, moneyStatementId);
	}

	@Override
	public List<FinanceMoneyStatementEntity> inquireFinanceMoneyReceiptStatementList(Long pickupId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.eq("statementSourceId", pickupId));
		criteria.add(Restrictions.in("statementSource", new String[] { "RECEIVABLES_WAREHOUSE", "QUARTER_SETTLEMENT_INTEREST" }));
		return null;
	}

	@Override
	public FinanceMoneyStatementEntity inquireFinanceMoneyStatementBySourceId(Long statementSourceId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceMoneyStatementEntity.class);
		criteria.add(Restrictions.eq("statementSourceId", statementSourceId));
		
		List<FinanceMoneyStatementEntity> fmses = PersistUtil.get(criteria);
		if (fmses != null && fmses.size() > 0) {
			return fmses.get(0);
		} else {
			return null;
		}
	}
}
